home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bisonpcb.zip / PRINT.C < prev    next >
C/C++ Source or Header  |  1987-02-13  |  5KB  |  235 lines

  1. /* Print information on generated parser, for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.   
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.   
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.   
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /*
  22.  * Port to PC by Whit Gregg
  23.  *               Nourse, Gregg & Browne, Inc.
  24.  *         1 Horizon Road
  25.  *         Fort Lee, NJ  07024
  26.  */
  27.  
  28.  
  29. #include <stdio.h>
  30. #include "machine.h"
  31. #include "new.h"
  32. #include "files.h"
  33. #include "gram.h"
  34. #include "state.h"
  35. #include "func.h"
  36.  
  37.  
  38.     extern char **tags;
  39. extern int nstates;
  40. extern short *accessing_symbol;
  41. extern core **state_table;
  42. extern shifts **shift_table;
  43. extern errs **err_table;
  44. extern reductions **reduction_table;
  45. extern char *consistent;
  46. extern char any_conflicts;
  47. extern char *conflicts;
  48.  
  49.  
  50.  
  51. void 
  52. terse()
  53. {                /* WG */
  54.     if (any_conflicts) {
  55.         conflict_log();
  56.         }
  57.     }
  58.  
  59.  
  60.  
  61. void 
  62. verbose()
  63. {                /* WG */
  64.     register int i;
  65.  
  66.     if (any_conflicts)
  67.         verbose_conflict_log();
  68.  
  69.     fprintf(foutput, "\n\ntoken types:\n");
  70.     print_token(-1, 0);
  71.     if (translations) {
  72.         for (i = 0; i <= max_user_token_number; i++)
  73.             /* Don't mention all the meaningless ones.  */
  74.             if (token_translations[i] != 2)
  75.                 print_token(i, token_translations[i]);
  76.         }
  77.     else
  78.         for (i = 1; i < ntokens; i++)
  79.             print_token(i, i);
  80.  
  81.     for (i = 0; i < nstates; i++) {
  82.         print_state(i);
  83.         }
  84.     }
  85.  
  86.  
  87.  
  88. void 
  89. print_token(extnum, token)    /* WG */
  90.     int extnum, token;
  91. {
  92.     fprintf(foutput, " type %d is %s\n", extnum, tags[token]);
  93.     }
  94.  
  95.  
  96.  
  97. void 
  98. print_state(state)        /* WG */
  99.     int state;
  100. {
  101.     fprintf(foutput, "\n\nstate %d\n\n", state);
  102.     print_core(state);
  103.     print_actions(state);
  104.     }
  105.  
  106.  
  107.  
  108. void 
  109. print_core(state)        /* WG */
  110.     int state;
  111. {
  112.     register int i;
  113.     register int k;
  114.     register int rule;
  115.     register core *statep;
  116.     register short *sp;
  117.     register short *sp1;
  118.  
  119.     statep = state_table[state];
  120.     k = statep->nitems;
  121.  
  122.     if (k == 0)
  123.         return;
  124.  
  125.     for (i = 0; i < k; i++) {
  126.         sp1 = sp = ritem + statep->items[i];
  127.  
  128.         while (*sp > 0)
  129.             sp++;
  130.  
  131.         rule = -(*sp);
  132.         fprintf(foutput, "    %s  ->  ", tags[rlhs[rule]]);
  133.  
  134.         for (sp = ritem + rrhs[rule]; sp < sp1; sp++) {
  135.             fprintf(foutput, "%s ", tags[*sp]);
  136.             }
  137.  
  138.         putc('.', foutput);
  139.  
  140.         while (*sp > 0) {
  141.             fprintf(foutput, " %s", tags[*sp]);
  142.             sp++;
  143.             }
  144.  
  145.         putc('\n', foutput);
  146.         }
  147.  
  148.     putc('\n', foutput);
  149.     }
  150.  
  151.  
  152.  
  153. void 
  154. print_actions(state)        /* WG */
  155.     int state;
  156. {
  157.     register int i;
  158.     register int k;
  159.     register int state1;
  160.     register int symbol;
  161.     register shifts *shiftp;
  162.     register errs *errp;
  163.     register reductions *redp;
  164.     register int rule;
  165.  
  166.     shiftp = shift_table[state];
  167.     redp = reduction_table[state];
  168.     errp = err_table[state];
  169.  
  170.     if (!shiftp && !redp) {
  171.         fprintf(foutput, "    NO ACTIONS\n");
  172.         return;
  173.         }
  174.  
  175.     if (shiftp) {
  176.         k = shiftp->nshifts;
  177.  
  178.         for (i = 0; i < k; i++) {
  179.             if (!shiftp->shifts[i])
  180.                 continue;
  181.             state1 = shiftp->shifts[i];
  182.             symbol = accessing_symbol[state1];
  183. /*      if (ISVAR(symbol)) break;  */
  184.             fprintf(foutput, "    %-4s\tshift  %d\n", tags[symbol], state1);
  185.             }
  186.  
  187.         if (i > 0)
  188.             putc('\n', foutput);
  189.         }
  190.     else {
  191.         i = 0;
  192.         k = 0;
  193.         }
  194.  
  195.     if (errp) {
  196.         k = errp->nerrs;
  197.  
  198.         for (i = 0; i < k; i++) {
  199.             if (!errp->errs[i])
  200.                 continue;
  201.             symbol = errp->errs[i];
  202.             fprintf(foutput, "    %-4s\terror (nonassociative)\n", tags[symbol]);
  203.             }
  204.  
  205.         if (i > 0)
  206.             putc('\n', foutput);
  207.         }
  208.     else {
  209.         i = 0;
  210.         k = 0;
  211.         }
  212.  
  213.     if (consistent[state] && redp) {
  214.         rule = redp->rules[0];
  215.         symbol = rlhs[rule];
  216.         fprintf(foutput, "    $default\treduce  %d  (%s)\n\n",
  217.             rule, tags[symbol]);
  218.         }
  219.     else if (redp) {
  220.         print_reductions(state);
  221.         }
  222.  
  223.     if (i < k) {
  224.         for (; i < k; i++) {
  225.             if (!shiftp->shifts[i])
  226.                 continue;
  227.             state1 = shiftp->shifts[i];
  228.             symbol = accessing_symbol[state1];
  229.             fprintf(foutput, "    %-4s\tgoto  %d\n", tags[symbol], state1);
  230.             }
  231.  
  232.         putc('\n', foutput);
  233.         }
  234.     }
  235.